package com.nancy.aop.aop;


import com.google.common.collect.Maps;

import java.io.Serializable;
import java.lang.reflect.Method;
import java.util.Map;

/**
 * All rights Reserved, Designed By guangfeng.zhou
 *
 * @version V1.0
 * @Project: learning-demo
 * @Package: com.nancy.aop
 * @Description: (用一句话描述该文件做什么)
 * @author: guangfeng.zhou
 * @date: 2017/12/1 16:39
 */
public class MethodCounter implements Serializable {

    private Map<String, Integer> map = Maps.newHashMap() ;

    private int allCount ;

    /**
     *
     * @param:
     * @return:
     */
    protected void count(Method m){
        count(m.getName()) ;
    }

    /**
     *
     * @param:
     * @return:
     */
    protected void count(String methodName){
        Integer i = map.get(methodName) ;
        i = (i!=null) ? new Integer(i.intValue()+1) : new Integer(1) ;
        map.put(methodName, i) ;
        allCount++ ;
    }

    /**
     * get methodName call times
     * @param:
     * @return:
     */
    public int getCalls(String methodName){
        Integer i = map.get(methodName) ;
        return  (i!=null) ? i.intValue() : 0 ;
    }

    /**
     * get all the method call times
     * @param:
     * @return:
     */
    public int getCalls(){
        return allCount;
    }

    @Override public boolean equals(Object o) {
        return (o!=null && o.getClass()==this.getClass() ) ;
    }

    @Override public int hashCode() {
        return getClass().hashCode();
    }
}
